<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
class Product extends Common
{
	public $intLinkSign;
	public $errorInfo;
	
	private $iUserID;
	
	private $objLog;

	function Product($strLanguage, $userid)
	{
		parent::Common();
		
		$this->intLinkSign = $this->getConnection($strLanguage);
		
		$this->objLog 	= new Logs($userid);
		$this->iUserID 	= $userid;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getFromID($arrPopedom = array(), $id, $languageid)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM products p, products_description pd WHERE p.id = pd.productID AND pd.languageID = $languageid AND p.id = $id";
		
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* 
	 *
	 * @param  string 	userid		
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 * @return int    	true: insert ID
	 */
    public function add($arrPopedom = array(), $categoryID, $price, $weight, $totalCount, $model, $manufacturerID, $taxRateID, $UPC, $URL, $languageid, $name, $description, $status = 'pause', $shippingTypeID)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
		
		$id		= 0;
		$weight = $weight * 100;
		$price 	= $price * 100;
		
		$sql = "INSERT INTO products SET createdTime = UTC_TIMESTAMP(), modifiedTime = UTC_TIMESTAMP(), price='$price', weight='$weight', status='$status'";
		$sql .= ", model='$model', manufacturerID='$manufacturerID', UPC = '$UPC', r_totalCount = '$totalCount', categoryID = $categoryID, shippingTypeID = $shippingTypeID";
		
				
		$rs  = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		if($rs){
			$id = mysql_insert_id($this->intLinkSign);
			
			$sql = "INSERT INTO products_description SET productID = $id, taxRateID = '$taxRateID', URL = '$URL', name='" . addslashes($name) . "', description='" . addslashes($description) . "', languageID = '$languageid'";
			
			$rs  = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
			if($rs){
				return $id;
			}else{
				$sql = "DELETE FROM products WHERE id = $id";
				$rs	 = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		
				return $id;
			}
		}else
			return $id;
	}

	/* 
	 *
	 * @param  string  	name 	
	 * @param  string 	namecn
	 * @param  string 	status 		normal, stop
	 * @return boolean 	true or fail
	 * 
	 */
	public function addAttribute($arrPopedom = array(), $productID, $attributeID, $attributePrice, $pricePrefix = '+')
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO products_attributes SET productID = '$productID', attributeID='$attributeID', attributePrice='$attributePrice', pricePrefix='$pricePrefix'";

		$rs = mysql_query($sql, $this->intLinkSign) or die($sql . mysql_error($this->intLinkSign));  
		
		if($rs){
			return mysql_insert_id($this->intLinkSign);
		}else
			return 0;
	}
	
	public function addAttributeOption($arrPopedom = array(), $productID, $attributeID, $attributeOptionID)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO products_attributes_options SET productID = '$productID', attributeID='$attributeID', attributeOptionID='$attributeOptionID'  ON DUPLICATE KEY UPDATE attributeOptionID=VALUES(attributeOptionID)";
		
		$rs  = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		$intDataID = mysql_insert_id($this->intLinkSign);
		if($rs)
			return $intDataID;
		else
			return false;
	}
	
	/* 
	 *
	 * @param  string  	name 	
	 * @param  string 	namecn
	 * @param  string 	status 		normal, stop
	 * @return boolean 	true or fail
	 * 
	 */
	public function addImage($arrPopedom = array(), $productID, $imageID, $attributeID, $isMain = 'no')
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO product_images SET productID = '$productID', imageID='$imageID', attributeID='$attributeID', isMain = '$isMain'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		
		return $rs ? true: false;
	}
	
	public function addImagetmp($productID, $imageID)
	{
		$sql = "INSERT IGNORE INTO product_images SET productID = '$productID', imageID='$imageID'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		
		return $rs ? true: false;
	}
	
	public function deleteImage($arrPopedom = array(), $productID, $imageID)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "DELETE FROM product_images WHERE productID = '$productID' AND imageID='$imageID'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		
		return $rs ? true: false;
	}
	
	public function deleteRelated($arrPopedom = array(), $productID)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "DELETE FROM products_related WHERE productID = '$productID'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		
		return $rs ? true: false;
	}
	
	public function productIDsOfImage($arrPopedom = array(), $imageID)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$productIDs = '';
		
		$sql = "SELECT productID FROM product_images WHERE imageID='$imageID'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				if(!empty($productIDs))
					$productIDs .= ',';
					
				$productIDs .= $arrtmp[0];
			}
		}
		
		return $productIDs;
	}
	
	/* edit
	 *
	 * @param  string 	userid		news class name 
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 	 * @param  return 	boolean
	 */
	public function edit($arrPopedom = array(), $id, $categoryID, $price, $weight, $totalCount, $model, $manufacturerID, $taxRateID, $UPC, $URL, $languageid, $name, $description, $status)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
		
		$weight = $weight * 100;
		$price 	= $price * 100;
		
		$sql = "UPDATE products SET modifiedTime = UTC_TIMESTAMP(), price='$price', weight='$weight', status='$status', model='$model', manufacturerID='$manufacturerID', UPC = '$UPC', r_totalCount = '$totalCount', categoryID = $categoryID WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		if($rs){
			$sql = "UPDATE products_description SET taxRateID = '$taxRateID', URL = '$URL', name='" . addslashes($name) . "', description='" . addslashes($description) . "', languageID = '$languageid' WHERE productID = $id";
			
			$rs  = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
			if($rs){
				return true;
			}else{
				return false;
			}
		}else
			return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id, $languageID)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id=$id");
		
		$sql = "SELECT count(*) FROM products_description WHERE productID = $id";

		$rs  = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		list($total) = mysql_fetch_row($rs);
		
		if($total == 1){
			$sql = "DELETE FROM products WHERE id = $id";
			$rs	 = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
			
			$sql = "DELETE FROM products_attributes WHERE productID = $id";
			$rs	 = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		}
		
		$sql = "DELETE FROM products_description WHERE productID = $id AND languageID = $languageID";
		$rs	 = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		
		if($rs)
			return true;
		else
			return false;
	}
	
	/* 
	 *
	 * @param  string  	name 	
	 * @param  string 	namecn
	 * @param  string 	status 		normal, stop
	 * @return boolean 	true or fail
	 * 
	 */
	public function deleteAttribute($arrPopedom = array(), $productID)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "DELETE FROM products_attributes WHERE productID = '$productID'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		
		return $rs ? true: false;
	}
	
	public function deleteAttributeOption($arrPopedom = array(), $productID)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "DELETE FROM products_attributes_options WHERE productID = '$productID'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		
		return $rs ? true: false;
	}
	
	/* edit user status
	 *
 	 * @ param  int    userid
	 * @ param  string status	
	 * @ param  return boolean
	 */
	public function setStatus($arrPopedom, $id, $status)
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_EDIT))
			die ($this->getNotPopedomInfo());
		
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "status=$status, id=$id");
			
		$sql = "UPDATE products SET status='$status' WHERE id=$id";
		$rs	 = mysql_query($sql,$this->intLinkSign);
		if(mysql_affected_rows() > 0)
			return true;
		else
			return false;
	}
	
	public function setIsNew($arrPopedom, $id, $isNew)
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_EDIT))
			die ($this->getNotPopedomInfo());
		
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "isNew=$isNew, id=$id");
			
		$sql = "UPDATE products SET isNew='$isNew' WHERE id=$id";
		$rs	 = mysql_query($sql,$this->intLinkSign);
		if(mysql_affected_rows() > 0)
			return true;
		else
			return false;
	}
	
	public function setIsHot($arrPopedom, $id, $isHot)
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_EDIT))
			die ($this->getNotPopedomInfo());
		
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "isHot=$isHot, id=$id");
			
		$sql = "UPDATE products SET isHot='$isHot' WHERE id=$id";
		$rs	 = mysql_query($sql,$this->intLinkSign);
		if(mysql_affected_rows() > 0)
			return true;
		else
			return false;
	}
	
	public function setIsFeatured($arrPopedom, $id, $isFeatured)
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_EDIT))
			die ($this->getNotPopedomInfo());
		
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "isFeatured=$isFeatured, id=$id");
			
		$sql = "UPDATE products SET isFeatured='$isFeatured' WHERE id=$id";
		$rs	 = mysql_query($sql,$this->intLinkSign);
		if(mysql_affected_rows() > 0)
			return true;
		else
			return false;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $languageid, $where, $order, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrback = array();

		$sql = "SELECT count(*) FROM products p, products_description pd WHERE p.id = pd.productID AND pd.languageID = $languageid $where";

		$rs  = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = "SELECT * FROM products p, products_description pd WHERE p.id = pd.productID AND pd.languageID = $languageid $where $order";
		else{
			$sql = "SELECT * FROM products p, products_description pd WHERE p.id = pd.productID AND pd.languageID = $languageid $where $order LIMIT $this->firstcount, $discount";
		}
		
		$rs  = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	public function IsExistName($languageID, $name)
	{
		$sql = "SELECT productID FROM products_description WHERE name = '$name' AND languageID = $languageID";
		$rs	 = mysql_query($sql, $this->intLinkSign) or die ("error: " . mysql_error($this->intLinkSign));
		if(mysql_num_rows($rs) > 0)
			return true;
		else
			return false;
	}
	




}
?>
